﻿using IXION.BLOG.COMMON.DATA;
using IXION.BLOG.DOMAIN.USER;
using NHibernate;
using NHibernate.Expression;
using System;
using System.Collections.Generic;
using System.Text;

namespace IXION.BLOG.DAO.USER
{
    public class UserDao
    {
        public User FindUserById(int id) {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(User));
            cr.Add(Expression.Eq("Id",id));
            IList<User> users = cr.List<User>();
            if(users.Count > 0){
                return users[0];
            }
            return null;
        }

        public void SaveOrUpdate(User user)
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ITransaction tr = session.BeginTransaction();
            try {
                session.SaveOrUpdate(user);
                session.Flush();
                tr.Commit();
            }catch(Exception ex){
                throw ex;
            }
            //throw new NotImplementedException();
        }

        public User FindUserByName(string userName)
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(User));
            cr.Add(Expression.Eq("UserName", userName));
            IList<User> users = cr.List<User>();
            if (users.Count > 0)
            {
                return users[0];
            }
            return null;
            //throw new NotImplementedException();
        }
    }
}
